<script>
export default {
  name:"Main",
  data() {
    let checkLoginName =(rule,value,callback)=>{
      if(this.form.id){
        return callback();
      }
      this.$axios.post(this.$httpUrl+"/zzh-staff/findeByLoginName?loginName="+this.form.loginName).then(res=>res.data).then(res=>{
        console.log(res.data)
        if(res.resultCode!=200){
          callback()
        }else{
          callback(new Error('登录账号已经存在'));
        }
      })
    };
    return {
      tableData: [],
      pageNum:1,
      pageSize:5,
      total:5,
      username:'',
      department:'',
      departments:[{
        value: '仓库部',
        label: '仓库部'
      }, {
        value: '采购部',
        label: '采购部'
      }, {
        value: '销售部',
        label: '销售部'
      }],
      dialogVisible:false,
      form:{
        username:'',
        loginName:'',
        position:'',
        department:'',
        email:'',
        phonenum:'',
        description:'',
        remark:''
      },
      rules: {
        loginName: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 4, max: 18, message: '长度在 4 到 18 个字符', trigger: 'blur' },
          {validator:checkLoginName,trigger: 'blur'}
        ],
        username: [
          { required: true, message: '请输入用户姓名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        position: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        department: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        phonenum: [
          {required: true,message: "⼿机号不能为空",trigger: "blur"},
          {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输⼊正确的⼿机号码", trigger: "blur"}
        ],
      }
    };
  },
  methods:{
    loadGet(){
      this.$axios.get(this.$httpUrl+'/zzh-staff/list').then(res=>res.data).then(res=>{
        console.log(res)
        if(res.resultCode==200){
          this.tableData=res.data
        }else {
          alert('获取数据失败')
        }
      })
    },
    loadPost(){
      this.$axios.post(this.$httpUrl+'/zzh-staff/list',{
        page:this.pageNum,
        limit:this.pageSize,
        username:this.username,
        department:this.department
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.resultCode==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }
      })
    },
    loadAdd(){
      //表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.form.id){
            this.doUpdate();
          }else {
            console.log("doAdd")
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    doAdd(){
      this.$axios.post(this.$httpUrl+'/zzh-staff/add',this.form).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.dialogVisible=false
          this.$message({
            showClose: true,
            message: '新增数据成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: '新增数据失败',
            type: 'error'
          });
        }
      })
    },
    doUpdate(){
      this.$axios.put(this.$httpUrl+'/zzh-staff/update',this.form).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.dialogVisible=false
          this.$message({
            showClose: true,
            message: '更新数据成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: '更新数据失败',
            type: 'error'
          });
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    resetParam(){
      this.username=''
      this.department=''
      this.loadPost()
    },
    add(){
      this.dialogVisible=true
      this.$nextTick(()=>{
        this.resetForm()
      })
    },
    mod(row){
      this.dialogVisible=true
      this.$nextTick(()=>{
        this.form.id=row.id
        this.form.username=row.username
        this.form.loginName=row.loginName
        this.form.position=row.position
        this.form.department=row.department
        this.form.phonenum=row.phonenum
        this.form.email=row.email
        this.form.description=row.description
        this.form.remark=row.remark
      })
    },
    del(id){
      this.$axios.delete(this.$httpUrl+'/zzh-staff/delete?id='+id).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: '删除失败',
            type: 'error'
          });
        }
      })
    },
    resetForm() {
      //重置窗体
      this.$refs.form.resetFields();
    }
  },
  beforeMount() {
    //this.loadGet();
    this.loadPost();
  }
}
</script>

<template>
  <div>
    <div style="margin-bottom: 5px">
      <el-input v-model="username" placeholder="请输入名字" suffix-icon="el-icon-search" style="width: 200px"
                @keyup.enter.native="loadPost"></el-input>
      <el-select v-model="department" filterable placeholder="请选择部门" style="margin-left: 5px">
        <el-option
            v-for="item in departments"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>

      <el-button type="primary" style="margin-left: 5px" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
      <el-button type="primary" style="margin-left: 5px" @click="add">新增</el-button>
    </div>

    <el-table :data="tableData"
              :header-cell-style="{background:'#f3f6fd',color:'#555'}">
      <el-table-column prop="id" label="id" width="140">
      </el-table-column>
      <el-table-column prop="loginName" label="登录名称" width="140">
      </el-table-column>
      <el-table-column prop="username" label="用户姓名" width="120">
      </el-table-column>
      <el-table-column prop="position" label="用户职位" width="120">
      </el-table-column>
      <el-table-column prop="department" label="部门" width="120">
      </el-table-column>
      <el-table-column prop="phonenum" label="电话号码" width="120">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120">
        <template slot-scope="scope">
          <el-tag
              :type="scope.row.status === 0 ? 'success' : 'danger'"
              disable-transitions>{{scope.row.status === 0 ? '正常' : '删除'}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定删除吗？"
              style="margin-left: 5px"
              @confirm="del(scope.row.id)"
          >
            <el-button slot="reference" size="small" type="danger">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[ 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="40%"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="mini">
        <el-form-item label="登录名称" prop="loginName">
          <el-input v-model="form.loginName" placeholder="请输入登录名称"></el-input>
        </el-form-item>
        <el-form-item label="用户姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户姓名"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="position">
          <el-select v-model="form.position" placeholder="请选择角色" style="width: 100%">
            <el-option label="采购员" value="采购员"></el-option>
            <el-option label="销售员" value="销售员"></el-option>
            <el-option label="仓库员" value="仓库员"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门" prop="department">
          <el-select v-model="form.department" placeholder="请选择机构" style="width: 100%">
            <el-option label="采购部" value="采购部"></el-option>
            <el-option label="销售部" value="销售部"></el-option>
            <el-option label="仓库部" value="仓库部"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" placeholder="请输入手机号" prop="phonenum">
          <el-input v-model="form.phonenum"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" placeholder="请输入电子邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="用户描述信息" placeholder="请输入用户描述信息" prop="description">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="备注" placeholder="请输入备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="loadAdd">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>